package com.ext.aic.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.ext.aic.R;
import com.ext.aic.adapter.ReferenceStyleListAdapter;
import com.ext.aic.apis.content.ContentGenerateRequest;
import com.ext.aic.beans.MenuItem;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.beans.params.ContentGenerateParams;
import com.ext.aic.beans.response.ContentGenerateResponse;
import com.ext.aic.components.MenuBottomPopup;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.databinding.CreateLayoutBinding;
import com.ext.aic.enums.EditType;
import com.ext.aic.utils.KeyboardUtils;
import com.ext.aic.utils.RedirectUtils;
import com.ext.aic.utils.UserUtils;
import com.ext.aic.utils.XPopupUtils;
import com.eyinfo.android_pure_utils.ebus.EBus;
import com.eyinfo.android_pure_utils.ebus.SubscribeEBus;
import com.mobsandgeeks.saripaar.annotation.NotEmpty;

import java.util.ArrayList;
import java.util.List;

public class CreateActivity extends BaseActivity {

    private CreateLayoutBinding binding;
    private boolean submitFlag;
    private ContentGenerateParams generateParams;

    @NotEmpty(message = "请输入文案内容描述")
    private EditText copywritingContentDescribeEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = View.inflate(this, R.layout.create_layout, null);
        binding = CreateLayoutBinding.bind(view);
        bindView();
        setContentView(binding.getRoot());
        EBus.getInstance().registered(this);
    }

    private void bindView() {
        binding.topView.titleTv.setText(R.string.create_title);
        binding.topView.backRl.setOnClickListener((v) -> finish());
        copywritingContentDescribeEt = binding.copywritingContentDescribeEt;
        binding.mediaChannelRl.setOnClickListener(this::onMediaChannelClick);
        binding.copywritingTypeRl.setOnClickListener(this::onCopywritingTypeClick);
        binding.copywritingContentDescribeEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                submitFlag = false;
                validatorHandler.validator.validate();
                binding.generateBtn.setEnabled(enableConfigCreate());
            }
        });
        binding.generateBtn.setOnClickListener(this::onGenerateBtnClick);
    }

    public void onGenerateBtnClick(View view) {
        submitFlag = true;
        validatorHandler.validator.validate();
    }

    public void onCopywritingTypeClick(View view) {
        KeyboardUtils.hideKeyboardFromView(this, view);
        List<MenuItem> items = new ArrayList<>();
        items.add(new MenuItem("产品推销", false));
        items.add(new MenuItem("个人品牌建设", false));
        items.add(new MenuItem("生活分享", false));
        MenuBottomPopup bottomPopup = new MenuBottomPopup(this, items);
        bottomPopup.setItemClickCall((item) -> {
            binding.copywritingTypeTv.setText(item.getName());
            binding.generateBtn.setEnabled(enableConfigCreate());
        });
        XPopupUtils.getInstance().showBottomDialog(this, bottomPopup);
    }

    public void onMediaChannelClick(View view) {
        KeyboardUtils.hideKeyboardFromView(this, view);
        List<MenuItem> items = new ArrayList<>();
        items.add(new MenuItem("小红书笔记", false));
        items.add(new MenuItem("微信朋友圈", false));
        items.add(new MenuItem("其它", false));
        MenuBottomPopup bottomPopup = new MenuBottomPopup(this, items);
        bottomPopup.setItemClickCall((item) -> {
            binding.mediaChannelTv.setText(item.getName());
            binding.generateBtn.setEnabled(enableConfigCreate());
        });
        XPopupUtils.getInstance().showBottomDialog(this, bottomPopup);
    }

    private boolean enableConfigCreate() {
        String channel = binding.mediaChannelTv.getText().toString();
        String type = binding.copywritingTypeTv.getText().toString();
        String describe = binding.copywritingContentDescribeEt.getText().toString();
        return !TextUtils.isEmpty(channel) && !TextUtils.isEmpty(type) && !TextUtils.isEmpty(describe);
    }

    @Override
    public void onValidationSucceeded() {
        if (submitFlag) {
            generateParams = new ContentGenerateParams();
            generateParams.setUserId(UserUtils.getInstance().getUserId());
            generateParams.setType(binding.copywritingTypeTv.getText().toString());
            generateParams.setPlatform(binding.mediaChannelTv.getText().toString());
            generateParams.setDescription(binding.copywritingContentDescribeEt.getText().toString());
            contentGenerateRequest.request(this, this, generateParams);
        }
    }

    private ContentGenerateRequest contentGenerateRequest = new ContentGenerateRequest() {
        @Override
        public void onContentGenerateSuccess(ContentGenerateResponse result) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("generateParams", generateParams);
            bundle.putSerializable("generateResult", result);
            bundle.putString("creationType", EditType.creation.name());
            RedirectUtils.getInstance().startActivity(CreateActivity.this, EditDetailActivity.class, bundle);
        }
    };

    @SubscribeEBus(receiveKey = {EBusConstants.finishCreate})
    public void onCreateFinished() {
        finish();
    }

    @SubscribeEBus(receiveKey = {EBusConstants.selectReferenceCopyBus})
    public void onSelectReferenceCopyEvent(ProductItem productItem) {
        binding.referenceStyleRv.setLayoutManager(new LinearLayoutManager(this));
        ReferenceStyleListAdapter adapter = new ReferenceStyleListAdapter();
        adapter.addItem(productItem);
        binding.referenceStyleRv.setAdapter(adapter);
        adapter.notifyItemInserted(0);
    }
}
